package com.cgc.venus.server.user.controller;

import com.cgc.venus.server.core.bean.response.VenusResponse;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.user.bean.FunctionBean;
import com.cgc.venus.server.user.inter.FunctionService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by xyr on 2017/9/12.
 * 用户中心_用户管理_功能菜单模块
 */

@Api(value = "功能菜单维护",description="功能菜单维护所对应服务的api")
@RequestMapping("/function-admin")
@RestController
public class FunctionController {

    @Autowired
    private FunctionService functionService;


    /*
    * 获得功能菜单树的根节点
    * */
    @ApiOperation(value = "获得功能菜单树的根节点接口", notes = "获得功能菜单树的根节点")
    @RequestMapping(value = "/root/node", method = RequestMethod.GET)
    public VenusResponse<Object> getRootNode() throws VenusException {
        List<Map<String,Object>> mapList = functionService.getRootNode();
        //int code = VenusResponseStatus.NOT_FOUND;
        //String message = "没在系统上找到功能菜单";
        if (mapList.size() > 0) {
            //code = VenusResponseStatus.OK;
            //message = "success";

            // 判断是否禁用
            for (Map<String,Object> map : mapList) {
                // 逐个判断是否禁用
                int active_int = (Integer) map.get("active");
                boolean active = active_int == 0 ? false : true;
                if (active) {
                    map.put("disabled",true);
                }
            }
        }
        VenusResponse<Object> response = new VenusResponse<Object>(VenusResponseStatus.OK,"",mapList);
        return  response;
    }


    /*
    * 获得功能菜单树的子节点
    * */
    @ApiOperation(value = "获得功能菜单树的子节点接口", notes = "获得功能菜单树的子节点")
    @RequestMapping(value = "/children/node/{id}", method = RequestMethod.GET)
    public VenusResponse<Object> getChildrenNode(@ApiParam("所属ID参数")@PathVariable("id") String id) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(id))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单所属ID为空");

        List<Map<String,Object>> mapList = functionService.getChildrenNode(id);
        //int code = VenusResponseStatus.NOT_FOUND;
        //String message = "没有子菜单";
        if (mapList.size() > 0) {
            //code = VenusResponseStatus.OK;
            //message = "success";

            // 判断是否禁用
            for (Map<String,Object> map : mapList) {
                // 逐个判断是否禁用
                int active_int = (Integer) map.get("active");
                boolean active = active_int == 0 ? false : true;
                if (active) {
                    map.put("disabled",true);
                }
            }
        }
        VenusResponse<Object> response = new VenusResponse<Object>(VenusResponseStatus.OK,"",mapList);
        return  response;
    }

    /*
    * 根据功能名字和功能所属的id判断是否存在相同的功能菜单
    * */
    /*@ApiOperation(value = "检查功能菜单是否存在接口", notes = "同一级菜单下不能有相同的子菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "功能菜单名字", paramType = "string"),
            @ApiImplicitParam(name = "belongId", value = "功能菜单所属ID", paramType = "string")
    })
    @RequestMapping(value = "/exist", method = RequestMethod.POST)
    public VenusResponse<String> exist(@RequestParam( value="name" ,required = true) String name,
                                       @RequestParam( value="belongId" ,required = true) String belongId ) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(name))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单名字为空");
        if (StringUtils.isBlank(belongId))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单所属ID为空");

        int code = VenusResponseStatus.NOT_FOUND;
        String message = "该功能菜单名称不存在";

        boolean isExist = functionService.exist(name, belongId);
        if (isExist) {
            code = VenusResponseStatus.OK;
            message = "该功能菜单名已经存在";
        }
        VenusResponse<String> response = new VenusResponse<String>(code,message,"");
        return response;
    }*/

    /*
    * 新增功能菜单
    * */
    @ApiOperation(value = "新增功能菜单接口", notes = "新增功能菜单接口:name,belongId,isMenu,url,active")
    @RequestMapping(value = "/function", method = RequestMethod.POST)
    public VenusResponse<Object> addFunction(@RequestBody FunctionBean functionBean) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(functionBean.getName()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单名字为空");
        if (StringUtils.isBlank(functionBean.getUrl()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单URL为空");
        // 200 正常
        // 601 数据重复
        int code = VenusResponseStatus.OK;
        String message = "新增功能菜单,成功";
        int row = 0;


        // 判断是否存在相同的数据
        boolean isExist = functionService.exist(functionBean.getName(), functionBean.getBelongId());

        if (isExist) {
            code = VenusResponseStatus.DATA_REPEAT;
            message = "新增的功能菜单,已经存在,请重新确认";
            throw new VenusException(code,message);
        } else {
            // 新增
            row = functionService.addFunction(functionBean);
        }

        VenusResponse<Object> response = new VenusResponse<Object>(code,message,"");
        return response;
    }


    /*
    * 根据功能ID,获得功能菜单信息
    * */
    @ApiOperation(value = "获得功能菜单基本信息接口", notes = "获得功能菜单基本信息")
    @RequestMapping(value = "/function/{id}", method = RequestMethod.GET)
    public VenusResponse<Object> getFunctionById(@ApiParam("所属ID参数")@PathVariable("id") String id) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(id))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能ID为空");

        int code = VenusResponseStatus.NOT_FOUND;
        String message = "在系统没找到该功能菜单信息";

        Map<String,Object> map = functionService.getFunctionById(id);

        if (map.size() > 0) {
            code = VenusResponseStatus.OK;
            message = "";

            int isMenu_int = (Integer) map.get("isMenu");
            int active_int = (Integer) map.get("active");

            boolean isMenu = isMenu_int == 0 ? false : true;
            boolean active = active_int == 0 ? false : true;

            map.put("isMenu",isMenu);
            map.put("active",active);
        } else {
            throw new VenusException(code,message);
        }
        VenusResponse<Object> response = new VenusResponse<Object>(code,message,map);
        return response;
    }

    /*
    * 修改功能信息
    * */
    @ApiOperation(value = "修改功能菜单接口", notes = "修改功能菜单:id,name,belongId,isMenu,url,active")
    @RequestMapping(value = "/function", method = RequestMethod.PATCH)
    public VenusResponse<Object> updateFunction(@RequestBody FunctionBean functionBean) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(functionBean.getId()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单ID为空");
        if (StringUtils.isBlank(functionBean.getName()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单名字为空");
        /*if (StringUtils.isBlank(functionBean.getBelongId()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单所属ID为空");*/
        if (StringUtils.isBlank(functionBean.getUrl()))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单URL为空");

        // 704 操作异常
        int code = VenusResponseStatus.HANDLE_ERROR;
        String message = "更新功能信息,失败";

        int row = functionService.updateFunction(functionBean);

        if (row > 0) {
            code = VenusResponseStatus.OK;
            message = "更新功能信息,成功";
        } else {
            throw new VenusException(code,message);
        }
        VenusResponse<Object> response = new VenusResponse<Object>(code,message,"");
        return response;
    }

    /*
    * 删除功能菜单信息
    * */
    @ApiOperation(value = "删除功能菜单基本信息接口", notes = "删除功能菜单基本信息")
    @RequestMapping(value = "/function/{id}", method = RequestMethod.DELETE)
    public VenusResponse<String> deleteFunction(@PathVariable("id") String id) throws VenusException {
        // 非空校验
        if (StringUtils.isBlank(id))
            throw new VenusException(VenusResponseStatus.NO_CONTENT,"功能菜单ID为空");
        // 先判断该功能菜单下有没有子菜单
        List<Map<String,Object>> mapList = functionService.getChildrenNode(id);

        // 返回状态码
        int code = VenusResponseStatus.OK;
        String message = "删除功能菜单,成功";
        if (mapList.size() > 0 ) {
            // 存在子菜单不能删除
            // 601
            code = VenusResponseStatus.DATA_REPEAT;
            message = "该功能菜单存在子菜单,不能删除";
        } else {
            // 删除
            int row = functionService.deleteFunction(id);
            if (row == 0) {
                // 操作异常
                // 704
                code = VenusResponseStatus.HANDLE_ERROR;
                message = "此次操作异常";
            }
        }

        if (code != VenusResponseStatus.OK) {
            throw new VenusException(code, message);
        }

        VenusResponse<String> response = new VenusResponse<String>(code,message,"");
        return response;
    }





}
